<?php 
/* this does somewhat the same as cache.php, but with the 
 * difference that it is made for loading external content
 * that does not use gzip compression or http caching headers
 * which causes the page speed to decrease */
require_once("config.php");

// external lib: Minify Classes incl. JSMin
require_once("lib/Minify/JSMin.class.php");
require_once("lib/Minify/Minify_HTML.class.php");
require_once("lib/Minify/Minify_Javascript.class.php");

// include minifcation class
require_once("class/minify.class.php");

// also use caching for the external content
require_once("class/cache.class.php");
require_once("class/database.class.php");
require_once("class/blog.class.php");

/* output gzip enable, when gzip is accepted */
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')){
	// client accepts gzip, so gzip the content
	ob_start("ob_gzhandler");
}else{ 
	// continue normally when gzip is unsupported
	ob_start();
}

// instanciate the blog and set global constants
$b = new blog($_SERVER["HTTP_HOST"]);

// blacklist of hosts to prevent from querying
$blacklist = array("scorecardresearch.com", "seg.sharethis.com");

// default mime is text/html
$mime = "text/html; charset=utf-8";

// define the url of the resource
$url = "http://".str_replace("/z/", "", $_SERVER['REQUEST_URI']);

// list of domains that required a redirect
$redirectList = array("www.amazon.de/dp/");
for($r=0; $r<count($redirectList);$r++){
	if(strpos($url,"http://".$redirectList[$r])!==false){
		header("Location: ".$url);
		exit();
	}
}

// determine the mime type based on extension
$ext = substr($url,strrpos($url, ".")+1);
if($ext=="css"){
	$mime="text/css";
}if($ext=="jpg"||$ext=="jpeg"){
	$mime="image/jpeg";
}if($ext=="png"){
	$mime="image/png";
}if($ext=="gif"){
	$mime="image/gif";
}

// default data is nothing
$data = "";
$is_blacklisted = false;

// go through the blacklist and check it
for($b=0;$b<count($blacklist);$b++){
	if(strpos($url, $blacklist[$b])!=false){
		$is_blacklisted = true;
	}
}

// only load data when the url is not blacklisted
if(!$is_blacklisted){
	$c = new cache();
	$cache_key = $c->hash("proxy_".$url);
	
	// check if the element exists in the local
	// server drive cache and return it from there
	// which is way faster than from the overused
	// 3rd party server systems
	if($c->has($cache_key)&&($ext=="png"||$ext="jpg"||$ext="jpeg"
							||$ext="gif"||$ext="css"||$ext="js")){
		$data = $c->get($cache_key);
	}else{
		// load the data of the external resource
		$data = file_get_contents($url);
		
		// replace urls so that they also use this proxy
		$data = str_replace("http://", "http://"
			.CONFIG_CDN_PROXY_DOMAIN."/z/", $data);
			
		// * fixes for specific files and sources
		// that replace also makes the sharethis redirect
		// malfunction so that the replace for the redirect
		// url needs to be reverted
		$data = str_replace("http://".CONFIG_CDN_PROXY_DOMAIN
								."/z/wd.sharethis.com", 
								"http://wd.sharethis.com", $data);
								
		// store it in the cache for one week, because
		// this is also the timeframe that we tell the
		// client to keep it cached locally
		if(($ext=="png"||$ext="jpg"||$ext="jpeg"
				||$ext="gif"||$ext="css"||$ext="js")){
			/* Warning on caching: Many of these scripts 
			 * use timestamps to make sure they are really
			 * loaded from their servers although it is not
			 * necessary. Putting all these items into the
			 * cache will cause the cache to get flooded.
			 * A flooded cache means longer server-side 
			 * execution time. Therefore we only cache
			 * css, js and images. */
			$c->put($cache_key, $data, time()+604800);
		}
	}
}

// define the mime
header("Content-Type: ".$mime);
	
// set the expires header to enforce caching
header("Cache-Control: max-age=604800");
header('Date: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', time()));
header("Expires: ".gmdate('D, d M Y H:i:s \G\M\T',(time()+604800)));

// minify the data when it is html
if(strpos($data, "<html")!=false){
	// minify the html
	$data = minify::minifyHtml($data);
}

// ouput the data
echo $data;
?>